$(document).ready(function() {
	// 设置各个输入框的提示
	$('input#AddressTextBox').setDefaultTip("可填写宿舍号");

	// 手机验证码的输入框隐藏
	$('ul#CodeInput').hide();

	$('input#AddressTextBox').focus(function() {
		// 显示宿舍地址提示输入
		VisitorAddressOnFocus();
	});

	$('input#UserNameTextBox').blur(function() {
		// 验证用户名是否已被注册
		CheckUserNameAvailable();
	});

	$('input#PhoneNumTextBox').blur(function() {
		// 验证电话号码
		CheckPhoneNum();
	});

	// 清除输入框tips
	$('input').focus(function() {
		WaiMai.ClearTips('RegisterTips');
	});
	$('input#UserNameTextBox').focus(function() {
		WaiMai.ClearTips('UserNameTip');
	});
	$('input#PhoneNumTextBox').focus(function() {
		WaiMai.ClearTips('PhoneNumTip');
		WaiMai.ClearTips('GetCodeTip');
	});
	$('input#AuthCodeTextBox').focus(function() {
		WaiMai.ClearTips('PhoneNumTip');
	});
});

// 显示地址提示
function VisitorAddressOnFocus() {
	if ($('input#AddressTextBox').val() == "") {
		var locationDiv = document.getElementById("locationChoose");
		locationDiv.style.left = (document.body.clientWidth / 2 + 100) + "px";
		$('div#locationChoose').css({
			'visibility' : 'visible'
		});
	}
}

// 验证用户名是否被注册
function CheckUserNameAvailable() {
	var userAvailable = false;
	var user = $('input#UserNameTextBox').val();
	if (user == "") {
	}
	$.ajax({
		async : false,
		type : "POST",
		url : "servlet/RegisteServlet",
		data : "flag=userName&value=" + user,
		error : function(error) {
		},
		success : function(response) {
			var result = response.trim();
			if (result == "UserAvailable") {
				userAvailable = true;
			} else if (result == "HasRegisted") {
				$('a#UserNameTip').html("该用户名已被注册");
			}
		}
	});
	return userAvailable;
}

// 验证电话号码是否是否正确且已被注册
function CheckPhoneNum() {
	var phoneAvailable = false;

	var phoneNum = $("input#PhoneNumTextBox").val();
	// 验证电话号码格式
	if (!WaiMai.UserCenter.IsRightPhoneNumFormat(phoneNum)) {
		$('a#PhoneNumTip').html("你输入的电话号码格式有误");
		return false;
	}
	$.ajax({
		async : false,
		type : "POST",
		url : "servlet/RegisteServlet",
		data : "flag=phone&value=" + phoneNum,
		error : function(error) {
		},
		success : function(response) {
			var result = response.trim();
			if (result == "PhoneNumAvailable") {
				phoneAvailable = true;
			} else if (result == "HaveUsed") {
				$('a#PhoneNumTip').html("你输入的号码已经被使用");
			}
		}
	});

	return phoneAvailable;
}
// 关闭送餐地址提示
function CloseChoose() {
	$('div#locationChoose').css({
		'visibility' : 'hidden'
	});
}

// add location to input box
function AddLocation(loc) {
	$('input#AddressTextBox').val(loc);
	$('input#AddressTextBox').focus();
	$('div#locationChoose').css({
		'visibility' : 'hidden'
	});
}

// 注册
function Register() {
	// 首先获得tips控件
	var tip = $('a#RegisterTips');

	var userName = $('input#UserNameTextBox').val();
	if (userName == "") {
		tip.attr("class", "Error");
		tip.html("用户名不能为空");
		return;
	}

	var password = $('input#PasswordTextBox').val();
	if (password == "") {
		tip.attr("class", "Error");
		tip.html("密码不能为空");
		return;
	}

	var address = $('input#AddressTextBox').val();

	var phoneNum = $('input#PhoneNumTextBox').val();
	if (phoneNum == "") {
		tip.attr("class", "Error");
		tip.html("手机号码不能为空");
		return;
	}

	if (!CheckUserNameAvailable()) {
		tip.attr("class", "Error");
		tip.html("你输入的用户名已被注册");
		return;
	}

	if (!CheckPhoneNum()) {
		tip.attr("class", "Error");
		tip.html($('a#PhoneNumTip').html());
		return;
	}

	tip.attr("class", "WaitingGray");
	tip.html("注册中……");

	$.ajax({
		type : "POST",
		url : "servlet/RegisteServlet",
		data : "flag=addUser&userName=" + userName + "&tel=" + phoneNum
				+ "&password=" + password + "&addr=" + address,
		error : function(error) {
			tip.attr("class", "Error");
			tip.html("系统错误，注册失败");
		},
		success : function(response) {
			var result = response.trim();
			if (result == "success") {
				tip.attr("class", "Right");
				tip.html("注册成功。自动跳转中……");
				window.location.href = "success.jsp?userName=" + userName;
			} else if (result == "failed") {
				tip.attr("class", "Error");
				tip.html("系统繁忙，注册失败");
			} else if (result == "HaveUsed") {
				tip.attr("class", "Error");
				tip.html("你输入电话号码已经被使用，请重新输入");
			} else if (result == "HasRegisted") {
				tip.attr("class", "Error");
				tip.html("你输入的用户名已被注册，请重新输入");
			}
		}
	});
}